package day12;

import java.util.Random;
import java.util.concurrent.locks.ReentrantReadWriteLock;

//a.银行账户余额查询与转账,使用ReentrantReadWriteLock
//实现银行账户系统，支持并发余额查询但互斥转账操作
//(提示: 提供三个方法 查询(读锁)  存款(写锁)  取款(写锁))
public class Bank {
    double money;//存款
    ReentrantReadWriteLock rwLock=
            new ReentrantReadWriteLock();
    public void select(){ //读锁
        String name=Thread.currentThread().getName();
        rwLock.readLock().lock();
        try {
            Thread.sleep(1000);
            System.out.println(name+"查询出余额:"+money);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            rwLock.readLock().unlock();
        }
    }
    public void deposit(double num){//写锁
        String name=Thread.currentThread().getName();
        rwLock.writeLock().lock();
        try {
            money+=num;
            Thread.sleep(1000);
            System.out.println(name+"存储了:"+num+",余额:"+money);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            rwLock.writeLock().unlock();
        }
    }
    public void atm(double num){//写锁
        String name=Thread.currentThread().getName();
        rwLock.writeLock().lock();
        try {
            if(money<num){
                System.out.println(name+"想取出:"+num+",但是余额不足");
                return;
            }
            money-=num;
            Thread.sleep(1000);
            System.out.println(name+"取出了:"+num+",余额:"+money);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            rwLock.writeLock().unlock();
        }
    }
    public static void main(String[] args) {
        Random r=new Random();
        Bank b=new Bank();
        for(int i=0;i<3;i++){
            new Thread(()->{
                for(int j=0;j<5;j++){
                    int n=r.nextInt(3);//0-2
                    switch (n){
                        case 0:b.select();break;
                        case 1:b.deposit((r.nextInt(100)+1)*100);break;
                        case 2:b.atm((r.nextInt(100)+1)*100);break;
                    }
                }
            },"线程:"+i).start();
        }
    }
}
